export class GameStateManager {
  constructor() {
    this.state = {
      currentScene: null,
      scenes: {},
      gameData: {
        highScore: 0,
        settings: {
          sound: true,
          music: true,
          vibration: true
        }
      }
    }
  }
  
  // 初始化游戏状态
  init() {
    // 从本地存储加载游戏数据
    try {
      const savedData = wx.getStorageSync('gameData')
      if (savedData) {
        this.state.gameData = JSON.parse(savedData)
      }
    } catch (e) {
      console.error('加载游戏数据失败:', e)
    }
  }
  
  // 保存游戏数据
  saveGameData() {
    try {
      wx.setStorageSync('gameData', JSON.stringify(this.state.gameData))
    } catch (e) {
      console.error('保存游戏数据失败:', e)
    }
  }
  
  // 更新最高分
  updateHighScore(score) {
    if (score > this.state.gameData.highScore) {
      this.state.gameData.highScore = score
      this.saveGameData()
    }
  }
  
  // 更新游戏设置
  updateSettings(settings) {
    this.state.gameData.settings = {
      ...this.state.gameData.settings,
      ...settings
    }
    this.saveGameData()
  }
  
  // 获取游戏设置
  getSettings() {
    return this.state.gameData.settings
  }
  
  // 保存游戏设置
  saveSettings(settings) {
    this.state.gameData.settings = settings
    this.saveGameData()
  }
  
  // 切换场景
  switchScene(sceneName) {
    if (this.state.currentScene) {
      this.state.currentScene.destroy()
    }
    
    const newScene = this.state.scenes[sceneName]
    if (newScene) {
      this.state.currentScene = newScene
      newScene.init()
    }
  }
  
  // 注册场景
  registerScene(name, scene) {
    this.state.scenes[name] = scene
  }
  
  // 获取当前场景
  getCurrentScene() {
    return this.state.currentScene
  }
  
  // 获取游戏数据
  getGameData() {
    return this.state.gameData
  }
} 